/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package th.stripes.services;

import com.google.inject.Inject;
import com.oas.thit._this.taha.xsd.TaHaProfileBody;
import com.oas.thit._this.taha.xsd.TaHaSchoolBody;
import com.oas.thit._this.taha.xsd.TaHaUniversityBody;
import com.oas.thit._this.xsd.THISBaseDataElementBody;
import com.oas.thit.xmlmessage.xsd.Header;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.bind.JAXBElement;
import org.apache.commons.lang.StringUtils;
import th.dao.KodFakultiDAO;
import th.dao.KodJantinaDAO;
import th.dao.KodJenisPengajianDAO;
import th.dao.KodJenisSekolahDAO;
import th.dao.KodNegeriDAO;
import th.dao.KodPengajianDAO;
import th.dao.KodSekolahDAO;
import th.dao.KodTahapPengajianDAO;
import th.dao.KodUniversitiDAO;
import th.dao.PoskodDAO;
import th.model.KodNegeri;
import th.model.Poskod;
import th.model.Taha;
import th.stripes.util.DateFormatDetector;
import th.stripes.util.Util;
import th.stripes.webservice.WebServiceUtil;

/**
 *
 * @author fikri
 */
public class TahaService {

     private static String SUCCESSFUL = "0000";

     private static String SYSTEM_ERROR = "9999";

     private static String TAHA_ENQ_MSG_ID = "TPTAPRQRWD01";

     private static String HEADER_ID_TAHA_ENQUIRY = "THSTAQR";

     @Inject
     private WebServiceUtil webUtil;

     @Inject
     private Util util;

     @Inject
     private KodUniversitiDAO kodUniversitiDAO;

     @Inject
     private KodJenisPengajianDAO kodJenisPengajianDAO;

     @Inject
     private KodTahapPengajianDAO kodTahapPengajianDAO;

     @Inject
     private KodSekolahDAO kodSekolahDAO;

     @Inject
     private KodNegeriDAO kodNegeriDAO;

     @Inject
     private KodJenisSekolahDAO kodJenisSekolahDAO;

     @Inject
     private PoskodDAO poskodDAO;

     @Inject
     private KodJantinaDAO kodJantinaDAO;

     @Inject
     private KodPengajianDAO kodPengajianDAO;

     @Inject
     private KodFakultiDAO kodFakultiDAO;

     public String enquiryTaha(Taha taha, String responseCode) throws ParseException {

          SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

          com.oas.thit._this.services.THISServices service = new com.oas.thit._this.services.THISServices();
          com.oas.thit._this.services.THISServicesPortType port = service.getTHISServicesHttpSoap11Endpoint();

//            LOG.debug( "WSDL Location" + port.toString());
          // TODO initialize WS operation arguments here
          com.oas.thit._this.xsd.THISRequest req = new com.oas.thit._this.xsd.THISRequest();
          // TODO process result here
          com.oas.thit._this.xsd.ObjectFactory objFactory = new com.oas.thit._this.xsd.ObjectFactory();
          com.oas.thit.xmlmessage.xsd.ObjectFactory objHeader = new com.oas.thit.xmlmessage.xsd.ObjectFactory();
          Header reqHead = objHeader.createHeader();
          reqHead.setMsgId(objHeader.createHeaderMsgId(TAHA_ENQ_MSG_ID));
          reqHead.setResponseCode(objHeader.createHeaderResponseCode(SUCCESSFUL));

          reqHead.setTxid(objHeader.createHeaderTxid(webUtil.getHeaderID(TAHA_ENQ_MSG_ID, HEADER_ID_TAHA_ENQUIRY)));

          THISBaseDataElementBody reqBody = objFactory.createTHISBaseDataElementBody();
          reqBody.setTHAcct(objFactory.createTHISBaseDataElementBodyTHAcct(taha.getNoAkaun()));
          reqBody.setTHICNo(objFactory.createTHISBaseDataElementBodyTHICNo(taha.getNoPengenalan()));

          JAXBElement<THISBaseDataElementBody> jReqBody = objFactory.createTHISRequestBody(reqBody);
          req.setBody(jReqBody);

          JAXBElement<Header> jReqHead = objHeader.createEventHeader(reqHead);
          req.setHeader(jReqHead);

          com.oas.thit._this.taha.xsd.TaHaProfileQueryResponse result = port.tahaProfileQuery(req);

          Header header = result.getHeader().getValue();
          responseCode = header.getResponseCode().getValue();

          if (responseCode.equals(SUCCESSFUL)) {
               TaHaProfileBody resp = result.getBody().getValue();
               taha.setNoAhli(resp.getTHTaHaNo().getValue());

               taha.setNoAkaun(resp.getTHAcct().getValue());
               taha.setNama(resp.getTHName().getValue());
               taha.setNoPengenalan(resp.getTHICNo().getValue());
               if (StringUtils.isNotBlank(resp.getTHDOB().getValue())) {
                    String df = new DateFormatDetector().getDateFormat(resp.getTHDOB().getValue());
                    sdf = new SimpleDateFormat(df);
                    taha.setTrhLahir(sdf.parse(resp.getTHDOB().getValue()));
                    Date today = new Date();
                    int age = util.getAge(resp.getTHDOB().getValue(), sdf.format(today), df);
                    taha.setUmur(age);
                    if (age < 17) {
                         taha.setJenisKelab("TaHa");
//                         getContext().getRequest().setAttribute("age", "1");
                    } else {
                         taha.setJenisKelab("TaHa@U");
//                         getContext().getRequest().setAttribute("age", "2");
                    }
               }
               taha.setJantina(kodJantinaDAO.findById(resp.getTHGender().getValue()));
               taha.setAlamat1(resp.getTHAddress1().getValue());
               taha.setAlamat2(resp.getTHAddress2().getValue());
               taha.setAlamat3(resp.getTHAddress3().getValue());
               if (StringUtils.isNotBlank(resp.getTHPoskod().getValue())) {
                    Poskod poskod = poskodDAO.findById(resp.getTHPoskod().getValue().substring(0, 5));
                    taha.setPoskod(poskod);
               }
               if (StringUtils.isNotBlank(resp.getTHState().getValue())) {
                    KodNegeri kodNegeri = kodNegeriDAO.findById(resp.getTHState().getValue());
                    taha.setNegeri(kodNegeri);
               }

               taha.setTelefon(resp.getTHPhone().getValue());
               taha.setEmail(resp.getTHemail().getValue());
               TaHaUniversityBody tub = resp.getMember18AndAbove().getValue();
               taha.setUniversiti(kodUniversitiDAO.findById(tub.getTHUniversity().getValue()));
               taha.setNoMatrik(tub.getTHMatrik().getValue());
               taha.setJenisPengajian(kodJenisPengajianDAO.findById(tub.getTHProgramtype().getValue()));
               taha.setTahapPengajian(kodTahapPengajianDAO.findById(tub.getTHLevel().getValue()));
               taha.setJurusanPengajian(StringUtils.isBlank(tub.getTHAcademicStream().getValue()) ? null : kodPengajianDAO.findById(tub.getTHAcademicStream().getValue()));
               taha.setFakultiPengajian(StringUtils.isBlank(tub.getTHFaculty().getValue()) ? null : kodFakultiDAO.findById(tub.getTHFaculty().getValue()));
               sdf = new SimpleDateFormat("yyyyMMdd");
               taha.setTrhmasukIPT(StringUtils.isNotBlank(tub.getTHAdmissionDate().getValue()) ? sdf.parse(tub.getTHAdmissionDate().getValue()) : null);
               sdf = new SimpleDateFormat("yyyyMM");

               taha.setTrhtamatIPT(StringUtils.isNotBlank(tub.getTHGradYear().getValue()) ? sdf.parse(tub.getTHGradYear().getValue()) : null);
               TaHaSchoolBody tsb = resp.getUnder18().getValue();
               taha.setSekolah(StringUtils.isBlank(tsb.getTHSchool().getValue()) ? null 
                       : kodSekolahDAO.findById(tsb.getTHSchool().getValue().trim()));
//                taha.setNegeriSekolah( StringUtils.isBlank(tsb.getTHSchoolState().getValue()) ? null : kodNegeriDAO.findById(tsb.getTHSchoolState().getValue().trim()) );
               if (taha.getSekolah() != null) {
                    taha.setJenisSekolah(taha.getSekolah().getJenisSekolah());
                    taha.setNegeriSekolah(
                            StringUtils.isBlank(taha.getSekolah().getNegeri().getCode()) ? null 
                                    : kodNegeriDAO.findById(taha.getSekolah().getNegeri().getCode().trim()));
               }

          }

          return responseCode;

     }
     
     
     

}
